/*
 * Created on 2005-6-3
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package cn.newtest.common.dbf;

import java.io.OutputStream;
import java.util.ArrayList;

import cn.newtest.common.exception.ApplicationException;

import org.apache.commons.lang.StringUtils;

/**
 * @author Administrator
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class StringToDBF {
	String spliter = ",", rowSpliter = "\r\n";

	public void setSpliter(String v) {
		spliter = v;
	}

	public void setRowSpliter(String v) {
		rowSpliter = v;
	}

	public boolean stringToDBF(String[] titles, StringBuffer str,
			String fileName, IFormater formater) throws ApplicationException {
		boolean success = true;
		String[] fields = null;
		JDBField[] jdbFields = null;
		if (titles != null) {
			jdbFields = getField(titles);
		} else {
			return false;
		}
		ArrayList dbData = formater.operate(str, titles.length, rowSpliter,
				spliter, jdbFields);
		try {
			writeFile(fileName, jdbFields, dbData);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new ApplicationException(ex.getMessage());
		}
		return success;
	}

	public boolean stringToDBF(String[] titles, StringBuffer str,
			OutputStream out, IFormater formater) throws ApplicationException {
		boolean success = true;
		String[] fields = null;
		JDBField[] jdbFields = null;
		if (titles != null) {
			jdbFields = getField(titles);
		} else {
			return false;
		}
		ArrayList dbData = formater.operate(str, titles.length, rowSpliter,
				spliter, jdbFields);
		try {
			writeFile(out, jdbFields, dbData);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new ApplicationException(ex.getMessage());
		}
		return success;
	}

	public JDBField[] getField(String[] titles) throws ApplicationException {
		JDBField[] jdbFields = new JDBField[titles.length];
		String[] fields = null;
		for (int i = 0; i < titles.length; i++) {
			fields = new String[titles.length];
			try {
				fields = StringUtils.split(titles[i], ":");
			} catch (Exception ex) {
				ex.printStackTrace();
				throw new ApplicationException(ex.getMessage());
			}
			if (fields.length != 5) {
				throw new ApplicationException("导出公式DBF文件字段定义错误");
			}
			int len = 0, pri = 0;
			if (fields[3].equals("B") || fields[3].equals("F")) {
				// 整数部分字符串
				String str_left = "";
				// 小数部分字符串
				String str_right = "";
				// 小数点
				String str_point = ".";
				// 小数点位置
				int ponitLocation = fields[4].indexOf(".");
				// 小数点后位数
				int pointLater = fields[4].length() - ponitLocation - 1;
				// 分离字符串为两部分
				if (ponitLocation >= 0) {
					str_left = fields[4].substring(0, ponitLocation);
					str_right = fields[4].substring(ponitLocation + 1);
					if (str_right.trim().length() == 0)
						str_right = "0";
				} else {
					str_left = fields[4];
					str_right = "0";
				}
				len = Integer.parseInt(str_left);
				pri = Integer.parseInt(str_right);
			} else {
				len = Integer.parseInt(fields[4].trim());
			}
			try {
				jdbFields[i] = new JDBField(fields[2], fields[3].charAt(0),
						len, pri);
			} catch (Exception ex) {
				ex.printStackTrace();
				throw new ApplicationException(ex.getMessage());
			}
		}
		return jdbFields;
	}

	public static void writeFile(java.io.OutputStream out,
			JDBField[] jdbFields, ArrayList al) throws Exception {
		DBFWriter dbfwriter = new DBFWriter(out, jdbFields, "UTF-8");
		Object[] obj = null;
		for (int i = 0; i < al.size(); i++) {
			obj = (Object[]) al.get(i);
			dbfwriter.addRecord(obj);
		}
		// dbfwriter.close();
	}

	public static void writeFile(String fileName, JDBField[] jdbFields,
			ArrayList al) throws Exception {
		DBFWriter dbfwriter = new DBFWriter(fileName, jdbFields, "UTF-8");
		Object[] obj = null;
		for (int i = 0; i < al.size(); i++) {
			obj = (Object[]) al.get(i);
			dbfwriter.addRecord(obj);
		}
		dbfwriter.close();
	}

	// public static void main(String[] args) {
	// }
}
